/*
 * Jenny Ruppen 09-730-391
 * Sebastien Goldberg 08-914-855
 * Henry Raymond 08-928-061
 * Stefan Mussato 09-752-270
 */
 
package ch.uzh.ifi.sopra.gruppe9.game;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import ch.uzh.ifi.sopra.gruppe9.game.BuyingAgent;


/**
 * Server-side class for the action location "house of spies", where you 
 * can buy ("bribe") tower elements from the pile of the tower element markets. 
 * Implements IActionLocation.
 */
@SuppressWarnings("serial")
public class HouseOfSpies extends UnicastRemoteObject implements IRmiActionLocation {

	
	/**
	 * Game board
	 */
	private IGameBoard gameBoard;
	
	
	/**
	 * Array of the slots where buying agent cards can be placed. 
	 * (0 = no card, 1 = one card in the color of the color constraint, 2 = two cards)
	 */
	private int[] buyingAgentSlots;
	
	
	/**
	 * Color constraint for buying agent cards in the house of spies.
	 */
	private BuyingAgent.Color colorConstraint = BuyingAgent.Color.NONE;
	
	
	/**
	 * Constructor
	 */
	public HouseOfSpies(IGameBoard gameBoard) throws RemoteException {
		super();
		
		this.gameBoard = gameBoard;
		
		buyingAgentSlots = new int[2];
		
		for(int i = 0; i < buyingAgentSlots.length; i++) {
			buyingAgentSlots[i] = 0;
		}
	}

	
	/**
	 * Returns array of the slots where buying agent cards can be placed. 
	 * (0 = no card, 1 = one card in the color of the color constraint, 2 = two cards)
	 */
	@Override
	public int[] getBuyingAgentSlots() throws RemoteException {
		return buyingAgentSlots;
	}

	
	/**
	 * Returns id of color of color constraint. (see enumToIntTranslator in class BuyingAgent)
	 */
	@Override
	public int getColorConstraint() throws RemoteException {
		return BuyingAgent.enumToInt(colorConstraint);
	}
	
	
	/**
	 * Bribes a market with the slot parameter1.
	 */
	@Override
	public boolean doAction(int playerIndex, int buyingAgentID1, int buyingAgentID2, int parameter1) throws RemoteException {
		
		IPlayer player = gameBoard.getPlayer(playerIndex);
		
		if(buyingAgentID2 == -1) { // One buying agent card
			
			if(colorConstraint != BuyingAgent.Color.NONE) { // Color constraint
				if(new BuyingAgent(buyingAgentID1).getColor() != colorConstraint) {
					System.err.println("Error: Color of buying agent card does not comply with color constraint.");
					return false;
				}
			} else {
				colorConstraint = new BuyingAgent(buyingAgentID1).getColor(); // Set color constraint
			}
			
			if(!player.removeBuyingAgent(buyingAgentID1)) {
				System.err.println("Error: Player doesn't have given buying agent card. (Buying agent ID: " + buyingAgentID1 + ")");
				return false;
			}
			
		} else { // Two buying agent cards
			System.err.println("Error: Cannot give two cards at the house of spies.");
			return false;
		}
		
		if(buyingAgentSlots[parameter1] != 0) { // Check if buying agent card slot is free.
			System.err.println("Error: Chosen buying agent slot is not free.");
			return false;
		}
		
		int[] costs = {3, 5};
		
		// Check if the player has enough money
		if(!player.checkMoney(costs[parameter1])) {
			System.err.println("Error: Player doesn't have enough money.");
			return false;
		}
	
		// Subtract the cost from the player's money balance
		player.payMoney(costs[parameter1]);
		
		// Put one buying agent cards on the chosen slot
		buyingAgentSlots[parameter1] = 1;
		
		gameBoard.reportUpdateMoneyBuyingAgentsInventory();
		
		return true;
	}
	
	
	/**
	 * Returns true if there is still at least one free buying agent card slot.
	 */
	public boolean hasFreeBuyingAgentSlot() throws RemoteException {
		for(int slot : getBuyingAgentSlots()) {
			if(slot == 0) {
				return true;
			}
		}
		return false;
	}
	
	
	/**
	 * Empties all buying agent slots.
	 */
	public void emptyBuyingAgentSlots() {
		colorConstraint = BuyingAgent.Color.NONE;
		for(int i = 0; i < buyingAgentSlots.length; i++) {
			buyingAgentSlots[i] = 0;
		}
	}
	
	
	/**
	 * Unused
	 */
	@Override
	public int[] getTowerElementSelectionIDs() throws RemoteException {
		return null;
	}
	
	
	/**
	 * Unused
	 */
	@Override
	public int[] getTowerElementPileIDs() throws RemoteException {
		return null;
	}
	

	/**
	 * Unused
	 */
	@Override
	public boolean doAction(int playerIndex, int buyingAgentID1, int buyingAgentID2) throws RemoteException {
		return false;
	}

	
	/**
	 * Unused
	 */
	@Override
	public boolean doAction(int playerIndex, int buyingAgentID1, int buyingAgentID2, int parameter1, int parameter2) throws RemoteException {
		return false;
	}
	
	
	/**
	 * Unused
	 */
	@Override
	public boolean doAction(int playerIndex, int buyingAgentID1, int buyingAgentID2, int parameter1, String parameter2) throws RemoteException {
		return false;
	}

}
